package com.yanqu.road.server.cmd.activity.dafuweng.cross;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;

@Cmd(code = GameProtocol.S_DA_FU_WENG_CROSS_UNION_SYNC_DOLL_MOVE, desc = "")
public class DfwDollSyncMoveCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MonopolyProto.SyncDollMoveMsg syncMsg = MonopolyProto.SyncDollMoveMsg.parseFrom(packet.getBytes());
        for (MonopolyProto.NotifyDollMoveMsg dollMoveMsg : syncMsg.getMsgList()) {
            String unionId = dollMoveMsg.getUnionId();
            for (CrossUnionMember member : new ArrayList<>(CrossUnionMgr.getUnionMemberMap(unionId).values())) {
                if (member.getServerId() == GameServer.getInstance().getServerId() && member.getUserId() != dollMoveMsg.getDollMsg().getUserId()) {
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                    if (player != null) {
                        MonopolyProto.SyncUnionMemberDollRespMsg.Builder respMsg = MonopolyProto.SyncUnionMemberDollRespMsg.newBuilder();
                        respMsg.addUnionMemberDoll(dollMoveMsg.getDollMsg());
                        player.sendPacket(ClientProtocol.U_DA_FU_WENG_SYNC_UNION_MEMBER_DOLL, respMsg);
                    }
                }
            }
        }
    }
}
